<template>
  <div>
    <el-dialog v-model="formDelete.dialog" title="确认" width="30%" center>
      <template #default>您将删除条目:{{ formDelete.name }}</template>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="formDelete.dialog = false">取消</el-button>
        <el-button type="danger" @click="ConfirmDelete">确定</el-button>
      </span>
      </template>
    </el-dialog>

    <el-dialog v-model="formAdd.dialog" :title="formAdd.PopupType" width="40%" center>
      <el-form :model="formAdd">
        <el-form-item label="图片">
          <el-upload
              class="avatar-uploader"
              action="/api/common/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess">
            <img v-if="formAdd.iconpath" class="avatar" :src="'/api/common/download?name='+formAdd.iconpath" alt=""/>
            <el-icon v-else>
              <Plus/>
            </el-icon>
          </el-upload>
        </el-form-item>

        <el-form-item label="名称">
          <el-input v-model="formAdd.content"/>
        </el-form-item>
        <el-form-item label="经度">
          <el-input v-model="formAdd.latitude"/>
        </el-form-item>
        <el-form-item label="纬度">
          <el-input v-model="formAdd.longitude"/>
        </el-form-item>
        <el-form-item label="介绍">
          <el-input type="textarea" v-model="formAdd.introduce"/>
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="formAdd.dialog=false">取消</el-button>
        <el-button type="primary" @click="addConfirm">确认</el-button>
      </span>
      </template>
    </el-dialog>

    <!--搜索框-->
    <div type="flex" style="margin-bottom: 10px;width: 100%;">
      <el-button type="primary" @click="addLandmarkInitialize">添加</el-button>
      <!--
            <el-button style="margin-left: 60px;" size="default" type="primary" :icon="Search" @click="getTableData"/>
            <el-button style="margin-left: 60px" size="default" type="primary" @click="OutExcel">导出表格</el-button>-->
    </div>
    <el-scrollbar>
      <el-table v-loading="loading" :header-cell-style="{background:'#eef1f6',color:'#606266'}" border
                :row-style="{height: '54.7px'}"
                style="font-size: 15px" stripe :data="tableData">
        <el-table-column label="图片" width="auto">
          <template #default="scopeEdit">
            <div class="demo-image">
              <div class="block">
                <el-image style="width: 100px; height: 100px" :src="'/api/common/download?name='+scopeEdit.row.iconpath"
                          fit="cover"/>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="名称" width="auto">
          <template #default="scopeEdit">
            <label> {{ scopeEdit.row.label.content }}</label>
          </template>
        </el-table-column>
        <el-table-column label="大小(宽x高)" width="auto">
          <template #default="scopeEdit">
            <label> {{ scopeEdit.row.width + "x" + scopeEdit.row.height }}</label>
          </template>
        </el-table-column>
        <el-table-column label="大小(纬,经)" width="auto">
          <template #default="scopeEdit">
            <label> {{ scopeEdit.row.latitude + "," + scopeEdit.row.longitude }}</label>
          </template>
        </el-table-column>

        <!--        <el-table-column prop="latitude" label="北纬" width="auto"/>
                <el-table-column prop="longitude" label="东经" width="auto"/>-->
        <el-table-column prop="phone" label="电话" width="auto"/>

        <el-table-column fixed="right" label="操作">
          <template #default="scopeEdit">
            <el-button
                link
                type="success"
                size="small"
                @click.prevent="editInitialize(scopeEdit.row)">
              <el-icon>
                <Edit/>
              </el-icon>
              <span>编辑</span>
            </el-button>
            <el-button
                link
                type="danger"
                size="small"
                @click.prevent="deleteInitialize(scopeEdit.row)">
              <el-icon>
                <Delete/>
              </el-icon>
              <span>删除</span>
            </el-button>
          </template>
        </el-table-column>

      </el-table>
    </el-scrollbar>

  </div>
</template>

<script lang="ts" setup>

import {onMounted, reactive, ref, watch} from "vue";
import {Delete, Edit, Operation, Plus, Search} from '@element-plus/icons-vue'


import {ElMessage, UploadProps} from "element-plus";
import {ElLoading} from "element-plus/es";
import {addLandmarkApi, deleteApi, getAllLandmarkApi, updateApi} from "@/api/Landmark";
import {deleteMenuApi} from "@/api/Menu";
import {calculateDistances} from "@/utils/utils";


//数据初始化
onMounted(() => {
  getTableData()
})

const tableData = ref([])
const loading = ref(true)

function getTableData() {

  getAllLandmarkApi().then((response: any) => {
    if (response.code === 200) {
      tableData.value = response.data
    } else {
      ElMessage.warning(response.msg)
    }
    loading.value = false
  }).catch(function (res) {
    loading.value = false
    ElMessage.error("页面加载失败")
  })

}


//添加系统用户
const PopupType = ref(['添加', '编辑'])
const formAdd = reactive({
  content: "",
  fontsize: 12,
  height: 50,
  iconpath: "",
  width: 50,
  introduce: "",
  latitude: "",
  longitude: "",
  textalign: "center",
  color: "#2BA1DF",
  dialog: false,
  PopupType: ''
})

function addLandmarkInitialize() {

  Object.assign(formAdd, {
    content: "",
    fontsize: 12,
    height: 50,
    iconpath: "",
    width: 50,
    introduce: "",
    latitude: "",
    longitude: "",
    textalign: "center",
    color: "#2BA1DF",
    dialog: true,
    PopupType: PopupType.value[0]
  })
}


function addConfirm() {
  if (formAdd.PopupType==PopupType.value[0]){
    addLandmarkApi(formAdd).then(function (response: any) {
      if (response.code === 200) {
        getTableData()
        formAdd.dialog = false
        ElMessage.success("操作成功")
      }
    }).catch(response => {
      ElMessage.error("操作失败")
    })
  }else{
    updateApi(formAdd).then(function (response: any) {
      if (response.code === 200) {
        getTableData()
        formAdd.dialog = false
        ElMessage.success("操作成功")
      }
    }).catch(response => {
      ElMessage.error("操作失败")
    })
  }

}


//添加修改数据初始化

//弹窗展示编辑表格
function editInitialize(row: any) {
  console.log(row)
  Object.assign(formAdd, {
    content: row.label.content,
    id: row.id,
    iconpath: row.iconpath,
    introduce: row.label.introduce,
    latitude: row.latitude,
    longitude: row.longitude,
    dialog: true,
    PopupType: PopupType.value[1]
  })
}


const handleAvatarSuccess: UploadProps['onSuccess'] = (
    response,
    uploadFile
) => {
  formAdd.iconpath = URL.createObjectURL(uploadFile.raw!)
  console.log(response)
  if (response.code == 200) {
    formAdd.iconpath = response.data
  }
}

const formDelete = reactive({
  id: -1,
  name: '',
  dialog: false
})

//删除
function deleteInitialize(row: any) {
  formDelete.id = row.id
  formDelete.name = row.label.content
  formDelete.dialog = true;
}

function ConfirmDelete() {
  const loading = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(0,0,0,0.7)',
  })
  deleteApi(formDelete.id)
      .then(function (response: any) {
        if (response.code === 200) {
          getTableData()
          ElMessage.success("删除成功")
        } else {
          ElMessage.error(response.message)
        }
        loading.close()
        formDelete.dialog = false;
      }).catch(response => {
    ElMessage.error("删除失败")
    loading.close()
  })
}
const formLabelWidth = '140px'
const inputSize = 'default'

</script>
